!MODULE nrutilA
!!! This module includes common functions for transformation
MODULE comf_trans
  USE NRUTIL, only : NR_BIG2
  IMPLICIT NONE
  
!  PRIVATE :: PRIV_INTA1,PRIV_CHEB_VAR,PRIV_CHEB_COEF,halfpi,P_Sort_Interp,P_Inssort_Interp ! Helper function, same as in matrix module
!  REAL(8), PARAMETER :: halfpi=1.57079632679489d0
     
CONTAINS
  
  real(8) function real2ab_exp(a_,b_,x_)
    !y = a + (b-a)/(1+exp(-x)), x is real, y is in (a, b)
    IMPLICIT NONE
    REAL(8), INTENT(IN) :: a_, b_, x_
    
    if (x_ <= - NR_BIG2 ) then
       real2ab_exp = a_
    elseif (x_ >= NR_BIG2 ) then
       real2ab_exp = b_
    else
       real2ab_exp = a_ + (b_ - a_) / (1.0d0 + exp(- x_))
    endif
    
  END function real2ab_exp
  
  real(8) function ab2real_exp(a_,b_,x_)
    ! solve for x in y = a + (b-a)/(1+exp(x)), x is real, y is in (a, b)
    ! x = - log((b-a)/(y-a) - 1)
    IMPLICIT NONE
    REAL(8), INTENT(IN) :: a_, b_, x_
    
    if ( x_ <= a_ ) then
       ab2real_exp = - NR_BIG2
    elseif ( x_ >= b_ ) then
       ab2real_exp = NR_BIG2
    else
       ab2real_exp = - log( (b_ - a_) / (x_ - a_) -1.0d0 )
    endif
    
  END function ab2real_exp     
END MODULE comf_trans
